<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="砍价商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="砍价商品id"></el-input>
    </el-form-item>
    <el-form-item label="砍价商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="砍价商品名称"></el-input>
    </el-form-item>
    <el-form-item label="活动开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="活动开始时间"></el-input>
    </el-form-item>
    <el-form-item label="活动结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="活动结束时间"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="countNum">
      <el-input v-model="dataForm.countNum" placeholder="限购数量"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="originalPrice">
      <el-input v-model="dataForm.originalPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商品底价" prop="basePrice">
      <el-input v-model="dataForm.basePrice" placeholder="商品底价"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则" prop="bargainingRules">
      <el-input v-model="dataForm.bargainingRules" placeholder="砍价规则"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="describes">
      <el-input v-model="dataForm.describes" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="规则标志值" prop="flag">
      <el-input v-model="dataForm.flag" placeholder="规则标志值"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodsId: '',
          goodsName: '',
          startTime: '',
          endTime: '',
          countNum: '',
          originalPrice: '',
          basePrice: '',
          bargainingRules: '',
          describes: '',
          flag: ''
        },
        dataRule: {
          goodsId: [
            { required: true, message: '砍价商品id不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '砍价商品名称不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '活动开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '活动结束时间不能为空', trigger: 'blur' }
          ],
          countNum: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ],
          originalPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          basePrice: [
            { required: true, message: '商品底价不能为空', trigger: 'blur' }
          ],
          bargainingRules: [
            { required: true, message: '砍价规则不能为空', trigger: 'blur' }
          ],
          describes: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          flag: [
            { required: true, message: '规则标志值不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargaindown/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.bargainDown.goodsId
                this.dataForm.goodsName = data.bargainDown.goodsName
                this.dataForm.startTime = data.bargainDown.startTime
                this.dataForm.endTime = data.bargainDown.endTime
                this.dataForm.countNum = data.bargainDown.countNum
                this.dataForm.originalPrice = data.bargainDown.originalPrice
                this.dataForm.basePrice = data.bargainDown.basePrice
                this.dataForm.bargainingRules = data.bargainDown.bargainingRules
                this.dataForm.describes = data.bargainDown.describes
                this.dataForm.flag = data.bargainDown.flag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargaindown/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'goodsId': this.dataForm.goodsId,
                'goodsName': this.dataForm.goodsName,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'countNum': this.dataForm.countNum,
                'originalPrice': this.dataForm.originalPrice,
                'basePrice': this.dataForm.basePrice,
                'bargainingRules': this.dataForm.bargainingRules,
                'describes': this.dataForm.describes,
                'flag': this.dataForm.flag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
